global source "ENTER ROOT DIRECTORY"

use "$source/sdc", clear
drop if date==. | year<1986 
gen month=month(date)

* keep mergers and 'takeovers'-> acq of assets, act of partial int., acq of major int.
* pool 'acq of remaining interest' with majority acq?
*keep if form=="Merger" | form=="Acq. of Assets" | form=="Acq. Maj. Int." | form=="Acq. Part. Int."  | form=="Acq. Part. Int"  
keep if form=="Merger" | form=="Acq. of Assets" | form=="Acq. Maj. Int." 

cap ssc install strrec
strrec form ("Acq. of Assets"="Acquisition: Assets") ("Acq. Maj. Int."="Acquisition: Majority") ("Acq. Part. Int."="Acquisition: Partial") ("Acq. Part. Int"="Acquisition: Partial"), replace
drop if k136=="Withdrawn"

* labor prod of acq needs to be matched from worldscope
keep if dscode_acq!="" 

gen sizeratio1= targetnetsalesltmmil/ acquirornetsalesmil
gen sizeratio2= targettotalassetsmil / acquirortotalassetsmil
gen size=cond(sizeratio1!=., sizeratio1, sizeratio2)

gen allcash=(m236==100)
gen allstock=(m238==100)
gen somestock=(m238!=.)
gen majstock=(m238!=. & m238>50)

gen publ_acq=regexm(a86, "Public")
gen publ_tar=regexm(t85, "Public")

ren k138_new dealvalue

gen crossborder=regexm(cb, "Y")
gen tender=regexm(t161, "Y")

replace asic=regexr(asic, "A", "0")
replace asic=regexr(asic, "B", "0")
replace asic=regexr(asic, "C", "0")
replace tsic=regexr(tsic, "A", "0")
replace tsic=regexr(tsic, "B", "0")
replace tsic=regexr(tsic, "D", "0")
replace tsic=regexr(tsic, "E", "0")
replace tsic=regexr(tsic, "F", "0")
destring asic tsic, replace
gen horizontal=(asic==tsic)
gen related1 = int(asic/100)==int(tsic/100) & !horizontal
gen related2 = int(asic/100)==int(tsic/100) 

keep year month dscode_acq dealno anation tnation form k136 s118 size all* *stock publ_* dealvalue crossborder tender horizontal aname related*

ren dscode_acq dscode

* match ws panel data into the sdc file
* do 8 merges to sequentially merge the (-3,4) years around the transaction, 4 years pre and post
foreach i in -3 -2 -1 0 1 2 3 4  {
	preserve
	replace year=year+`i'
	merge m:1 dscode year using $source/ws_all.dta, keep(1 3) gen(_ds) keepusing(id sic1 dprodsic4 prod TA salaries matex ppe sales marketcap totdebt prof)
	save y`i', replace
	restore
	}

clear
gen time=.
foreach i in -3 -2 -1 0 1 2 3 4 {
	append using y`i'
	replace time=`i' if time==.
	erase y`i'.dta
	}
	
keep if year>1985 & year<2011
xtset dealno year

	************ labour prod, ROA and tobin's q

gen tobinsq=(marketcap + totdebt)/TA
replace tobinsq=. if tobinsq>50 | time>1 // since Q is forward looking, only use from t+1

gen post=(time>0)

foreach var in prod prof tobinsq salaries {
	by dealno: egen pre`var'1=mean(`var') if !post
	by dealno: egen post`var'1=mean(`var') if post
	by dealno: egen pre`var'2=max(pre`var'1) 
	by dealno: egen post`var'2=max(post`var'1) 
	}

gen deltaLP=postprod2/preprod2
gen deltaROA=postprof2/preprof2
gen deltaTOBI=posttobinsq2/pretobinsq2
gen deltaSALA=postsalaries2/presalaries2

su deltaSALA,d
tabstat deltaSALA if deltaSA<r(p99) & deltaSA>r(p1), by(form) stat(mean sd) notot labelwidth(21)
*drop deltaSALA

	************ TFP productivity measures

foreach var in TA sales ppe matex salaries {
	gen ln`var'=log(`var')
	drop `var'
	}
gen sic3=int(sic1/10)

*** Cobb Douglas TFP
gen TFP=.
qui levelsof sic3, clean
foreach sic in `r(levels)' {
	cap reg lnsales lnTA lnsalaries if sic3==`sic'
	cap predict pred if e(sample)
	cap replace TFP=lnsales-pred if e(sample)
	cap drop pred
	}

*** Cobb Douglas TFP with firm fixed effects
gen TFPFE=.
qui levelsof sic3, clean
foreach sic in `r(levels)' {
	cap areg lnsales lnTA lnsalaries if sic3==`sic', absorb(id)
	cap predict pred if e(sample)
	cap replace TFPFE=lnsales-pred if e(sample)
	cap drop pred
	}	
	
*** Second-order translog TFP with FE
gen TFPTL=.
gen ln2TA=lnTA^2
gen ln2sal=lnsalaries^2
gen lnTAsal=lnTA*lnsalaries
qui levelsof sic3, clean
loc i 0
foreach sic in `r(levels)' {
	cap areg lnsales lnTA lnsalaries ln2TA ln2sal lnTAsal if sic3==`sic', absorb(id)
	loc a=_rc
	if `a'==0 	noi di `++i'
	else di "no obs"
	cap predict pred if e(sample)
	cap replace TFPTL=lnsales-pred if e(sample)
	cap drop pred
	}
	
foreach x in TFP TFPFE TFPTL {
	by dealno: egen pre`x'1=mean(`x') if !post
	by dealno: egen post`x'1=mean(`x') if post
	by dealno: egen pre`x'2=max(pre`x'1) 
	by dealno: egen post`x'2=max(post`x'1)
	gen delta`x'=post`x'2-pre`x'2
	}

replace dprodsic4=dprodsic4/100
bysort sic1 year: egen dTFPsic4=mean(TFP)
bysort sic1 year: egen dTFPFEsic4=mean(TFPFE)
bysort sic1 year: egen dTFPTLsic4=mean(TFPTL)

label var dprodsic4 "$\overline{\text{LP}}_{i, t}$"
label var dTFPsic4 "$\overline{\text{TFP}}_{i, t}$"
label var dTFPsic4 "$\overline{\text{TFP}}_{i, t}$"
label var dTFPsic4 "$\overline{\text{TFP}}_{i, t}$"

keep if time==0 & (deltaLP!=. | deltaTFP!=.)

* add CARs from Eventus
merge 1:1 dealno using $source/CARdata, keepusing(CAR* VXOClose)
label var VX "VXO Close"
gsort -_merge dealno

* add decomposed VXO (measure for risk aversion)
merge m:1 year month using $source/vxo_decomp, keep(1 3) nogen

* there are deals that are duplicates in everything but dealno, drop
duplicates drop sic1 year id form k136 delta* anation tnation, force

codebook dscode if TFPTL!=.
codebook anation if TFPTL!=.
tab anation if TFPTL!=., sort
tab crossborder if TFPTL!=.
codebook sic1 if TFPTL!=.

gen acquicount=.
levelsof year, local(year)
foreach y of local year {
	local z = `y'-4
	*tempfile  acquicount`y'
	bysort dscode sic1: egen acquicount`y'=sum(year<=`y' & year>=(`z'))
	replace acquicount= acquicount`y' if year==`y'
	drop acquicount`y'
	}
* code acquicount binary
replace acquicount=0 if acquicount==1
replace acquicount=1 if acquicount>1	

keep year month dealno anation tnation form id sic1 prod dprodsic4 delta* CAR* VXOClose size* all* somestock k136 s118 dTFP* publ_* dealvalue crossborder tender horizontal VRP acquicount lnTA related*
compress

label var publ_acq "Public Acquirer"
label var publ_tar "Public Target"
label var crossborder "Crossborder Deal"
label var tender "Tender Offer"
label var horizontal "Horizontal Deal"
label var dTFPTLsic4 "$\overline{\text{TFP}}_{i, t}$"
label var acquicount "Multiple deals"

gen int sic2=sic1/100

preserve

	use "$source/ws_all.dta", clear
	keep sic1 year rd sales TA emp
	gen int sic2=sic1/100
	
	gen rdint=rd/sales
	su rdint,d
	replace rdint=. if rdint<r(p1) | rdint>r(p99)
	bysort sic2: egen rdcount=count(rdint)
	su rdcount,d
	replace rdint=. if rdcount<r(p5) 
	bysort sic2: egen mrdint=mean(rdint)
	
	gen labint=emp/sales
	su labint,d
	replace labint=. if labint<r(p1) | labint>r(p99)
	bysort sic2: egen labcount=count(labint)
	su labcount,d
	replace labint=. if labcount<r(p5) 
	bysort sic2: egen mlabint=mean(labint)

	gen capint=TA/sales
	su capint,d
	replace capint=. if capint<r(p1) | capint>r(p99)
	bysort sic2: egen capcount=count(capint)
	su capcount,d
	replace capint=. if capcount<r(p5) 
	bysort sic2: egen mcapint=mean(capint)
		
	keep sic2 mrdint mlabint mcapint
	duplicates drop
	
	su mrdint,d
	gen highrd=mrdint>r(p75) & mrdint!=.
	su mlabint,d
	gen highlab=mlabint>r(p75) & mlabint!=.
	su mcapint,d
	gen highcap=mcapint>r(p75) & mcapint!=.

	tempfile rdint
	save `rdint'

restore

merge m:1 sic2 using `rdint', nogen
drop mlabint mcapint highrd highlab highcap k136 s118
drop if year==.

su mrdint, d
foreach p in 10 25 50 75 90 {
	gen rdint`p' = mrdint>=r(p`p')
	}

save sample, replace

*
**********************************************************************************************************************************************************************
**********************************************************************************************************************************************************************
**********************************************************************************************************************************************************************
**********************************************************************************************************************************************************************
*

cap erase "fig/reg_main.rtf"

foreach var in deltaTFPTL CAR {
	global outcome `var'
	
	qui {	
		
use sample, clear

* drop top percentile and bottom percentile of outcome
su $outcome, d
gen out=($outcome>r(p99) | $outcome<r(p1))

if "$outcome"=="deltaTFPTL" {
		gen sample=!mi(anat, tnat, sic1, $outcome, dTFPTLsic4) & !out
		}							
else 	gen sample=!mi(anat, tnat, sic1, $outcome, dprodsic4) & !out

* add controls
merge m:1 sic1 year using source/indavg, keep(3) nogen
su ind_logtot_netinc
replace ind_logtot_netinc=r(mean) if sample & ind_logtot_netinc==.
su ind_logtot_ltdebt
replace ind_logtot_ltdebt=r(mean) if sample & ind_logtot_ltdebt==.

if 			"$outcome"=="deltaTFPTL" global dprod "dTFPTLsic4"
*else if 	"$outcome"=="deltaTFPFE" global dprod "dTFPFEsic4"
else 								 global dprod "dprodsic4"
global control publ_acq publ_tar crossborder tender horizontal acquicount related2

* REVISION: ONLY CASH VS STOCK FUNDED DEALS
keep if allstock | allcash
drop if allstock & allcash

if "$outcome"=="deltaTFPTL" {
		noi sum $control $dprod ind* if sample
		}
			
encode anation, gen(nat1)
encode tnation, gen(nat2)
	
noi su $outcome if sample

* instruments
global instruments "expansionyear share_avg"	
* 'strict' wave definition (>p99) work best
merge m:1 sic1 year using $source/wavestart_new, keep(1 3) nogen 
gen sic=int(sic1/100)
merge m:1 sic year using $source/deregulation_shocks, keep(1 3) nogen
recode wave* shock* (.=0)
* generate share-of-mergers in non-focal industry as instrument
* this would run much faster in mata
gen share_avg=.
gen insample=1
levelsof sic1, local(inds) clean
qui foreach ind of local inds {
	di "`ind'"
	replace insample=0 if sic1==`ind'
	bysort year: egen ms=	mean(allstock) if insample
	bysort year: egen ms2=	max(ms)
	replace share_avg=ms2 if sic1==`ind'
	drop ms ms2
	replace insample=1
	}

		************************************************************
		*********** Proposition 1: Impact of mergers on mean outcome
		************************************************************
		
if "$outcome"!="CAR" {
	eststo mean$outcome$class: areg $outcome allstock $dprod $control i.year i.nat1 i.nat2 ind* if sample, absorb(sic1)
	noi su allstock if e(sample)
	
eststo highlowrd10: areg $outcome 1.allstock#1.rdint10 $dprod $control i.year i.nat1 i.nat2 ind* if sample, absorb(sic1)
eststo highlowrd25: areg $outcome 1.allstock#1.rdint25 $dprod $control i.year i.nat1 i.nat2 ind* if sample, absorb(sic1)
eststo highlowrd50: areg $outcome 1.allstock#1.rdint50 $dprod $control i.year i.nat1 i.nat2 ind* if sample, absorb(sic1)
eststo highlowrd75: areg $outcome 1.allstock#1.rdint75 $dprod $control i.year i.nat1 i.nat2 ind* if sample, absorb(sic1)
eststo highlowrd90: areg $outcome 1.allstock#1.rdint90 $dprod $control i.year i.nat1 i.nat2 ind* if sample, absorb(sic1)
	}

	
if "$outcome"!="CAR" {
egen sicyear=group(sic1 year)
bysort sicyear: egen share_allstock=mean(allstock)
label var share_allstock "Share of stock-funded transactions"

gen p25=1.allstock#1.rdint25
gen p50=1.allstock#1.rdint50
gen p75=1.allstock#1.rdint75
	
	qui eststo sp1$outcome: etregress $outcome $dprod $control i.year i.nat1 i.nat2 i.sic1 ind* if sample, treat(allstock= $instruments share_allstock)
	noi esttab sp1$outcome, drop(*.year *.nat* ind* *.sic*) tit(outcome variable: ETregress $outcome) nonotes nonum wide star(* 0.1 ** 0.05 *** 0.01) order(*allstock)  

	qui eststo sp1highlowrd25: etregress $outcome $dprod $control i.year i.nat1 i.nat2 i.sic1 ind* if sample, treat(p25= expansionyear share_allstock)
	qui eststo sp1highlowrd50: etregress $outcome $dprod $control i.year i.nat1 i.nat2 i.sic1 ind* if sample, treat(p50= expansionyear share_allstock)
	qui eststo sp1highlowrd75: etregress $outcome $dprod $control i.year i.nat1 i.nat2 i.sic1 ind* if sample, treat(p75= expansionyear share_allstock)
	noi esttab sp1highlowrd25, drop(*.year *.nat* ind* *.sic*) tit(outcome variable: ETregress $outcome) nonotes nonum wide star(* 0.1 ** 0.05 *** 0.01) order(*allstock)  
	noi esttab sp1highlowrd50, drop(*.year *.nat* ind* *.sic*) tit(outcome variable: ETregress $outcome) nonotes nonum wide star(* 0.1 ** 0.05 *** 0.01) order(*allstock)  
	noi esttab sp1highlowrd75, drop(*.year *.nat* ind* *.sic*) tit(outcome variable: ETregress $outcome) nonotes nonum wide star(* 0.1 ** 0.05 *** 0.01) order(*allstock)  
	}	
		
		********************************************************************
		*********** Proposition 3: Mergers, risk aversion and stock reaction
		********************************************************************
	
if "$outcome"=="CAR" {
	noi su VXOClose if sample,d
 	gen VXOmedian=(VXOClose>=r(p50)) if sample
	eststo car1$outcome: areg $outcome allstock 1.allstock#1.VXOmedian VXOClose	i.year i.nat1 i.nat2 ind* publ_tar crossborder tender horizontal acquicount related2 if sample, absorb(sic1)
		
	egen sicyear=group(sic1 year)
	bysort sicyear: egen share_allstock=mean(allstock)
	label var share_allstock "Share of stock-funded transactions"
	
	eststo sp3$outcome: ivregress 2sls $outcome  VXOClose	i.year i.nat1 i.nat2 i.sic1 ind* $control (allstock 1.allstock#1.VXOmedian = $instruments share_allstock) if sample
	qui eststo sp3VXO$outcome: etregress $outcome 1.allstock#1.VXOmedian VXOClose 	i.year i.nat1 i.nat2 i.sic1 ind* publ_tar tender horizontal related2 acquicount crossborder if sample, treat(allstock= $instruments share_allstock)
	esttab sp3$outcome, drop(*.year *.nat* ind* *.sic*) nonotes nonum wide star(* 0.1 ** 0.05 *** 0.01) order(*allstock)  	
	}		

	
			*************************************************************
			*********** Proposition 2: Variance of change in productivity
			*************************************************************

if "$outcome"!="CAR" {

	drop if out

	* unit of observation changes from deal to sic4/year cluster
	cap drop sicyear
	egen sicyear=group(sic1 year)
	bysort sicyear: gen N=_N

	by sicyear: egen clustersd=sd($outcome)
	by sicyear: ereplace crossborder=mean(crossborder)

	cap drop share_allstock
	by sicyear: egen share_allstock=mean(allstock)
	label var share_allstock "Share of stock-funded transactions"
	sort sicyear

	drop if sicyear==sicyear[_n-1] | sic1==. | N==1 | clustersd==0
	xtset sic1 year
	tsfill

	* FEs for acq/tar nationality do not make sense here
	eststo sd$outcome: areg clustersd share_allstock ind* i.year, absorb(sic1)
	
	eststo sp2$outcome: ivregress 2sls clustersd ind* i.year i.sic1 (share_allstock= $instruments ), first
	estat endogenous
	estadd scalar Durbin = r(durbin), :sp2$outcome$class
	estadd scalar pDurbin = r(p_durbin), :sp2$outcome$class
	estadd scalar Wu = r(wu), :sp2$outcome$class
	estadd scalar pWu = r(p_wu), :sp2$outcome$class

	estat firststage
	mat a=r(singleresults)
	estadd scalar F=a[1, 4], :sp2$outcome$class
	estadd scalar pF=a[1, 7], :sp2$outcome$class

	estat overid
	estadd scalar Sargan=r(sargan), :sp2$outcome$class
	estadd scalar pSargan=r(p_sargan), :sp2$outcome$class
	estadd scalar Basmann=r(basmann), :sp2$outcome$class
	estadd scalar pBasmann=r(p_basmann), :sp2$outcome$class
	}
}

	
noi di "-----------------------------------------------------------------------------------------------"
noi di "Outcome variable: $outcome"

*global mtit "mtit(SDC SDC-Stock SDC-Size Standard-Stock Standard-Size Stock-Swap Stock-vs-Cash)"
global opt  "star(* 0.1 ** 0.05 *** 0.01) nonotes b(%8.3f) se(%6.2f) align(c) compress nogaps nodep nonum append wide"

* display results
if "$outcome"!="CAR" noi esttab mean*, 	drop(*.* ind*) tit(outcome variable: $outcome) nonotes nonum 
if "$outcome"!="CAR" noi esttab sd*, 	drop(*.* ind*) tit(outcome variable: $outcome ) nonotes nonum 
if "$outcome"!="CAR" noi esttab highlow*, 	drop(*.nat* *.year ind*) tit(outcome variable: $outcome ) nonotes nonum 
if "$outcome"=="CAR" noi esttab car1*, 	drop(*.nat* *.year) tit(CAR - VXO) nonotes nonum star(* 0.1 ** 0.05 *** 0.01) order(allstock 1.allstock* VXO* publ* crossborder tender horizontal ind*) rename(1.Merger Merger*VXOmedian)

label var dprodsic4 "Industry LP"  
label var dTFPTLsic4 "Industry TFP"
label var ind_logtot_TA "Industry-Assets"
label var ind_logtot_sales "Industry-Sales"
label var ind_logtot_netinc "Industry-Income"
label var ind_logtot_emp "Industry-Employees"
label var ind_logtot_ltdebt "Industry-Debt"
label var related2 "Related Industry"

* write to rtf file
if "$outcome"!="CAR" esttab mean* 	using "fig/reg_main.rtf", $opt drop(*.*) order(allstock $dprod publ* crossborder tender horizontal related2 ind*) label

if "$outcome"!="CAR" esttab sd* 	using "fig/reg_main.rtf", $opt drop(*.*) label

if "$outcome"!="CAR" noi esttab highlow* using "fig/reg_main.rtf", star(* 0.1 ** 0.05 *** 0.01) nonotes b(%8.3f) se(%6.2f) align(c) compress nogaps nodep nonum append  drop(*.nat* *.year ind*) nonotes nonum  order(1.allstock*)

if "$outcome"!="CAR" esttab sp1$outcome 		using "fig/reg_main.rtf", $opt  drop(*.year *.sic* *.nat* ) order(1.allstock dTFP crossborder tender horizontal related2 publ_tar publ_acq ind*) eqlabels("Second stage: Productivity" "First stage: Selection") label

if "$outcome"!="CAR" esttab sp2$outcome* 		using "fig/reg_main.rtf", $opt  drop(*.*) order(*Merger*) scalars(F pF Durbin pDurbin Wu pWu Sargan pSargan Basmann pBasmann) label


if "$outcome"=="CAR" esttab car1* 	using "fig/reg_main.rtf", $opt drop(*.nat* *.year) order(1.allstock* allstock* VXOmedian VXO* publ* crossborder tender horizontal related2 ind*) label

if "$outcome"=="CAR" esttab sp3VXO$outcome* 	using "fig/reg_main.rtf", $opt drop(*.year *.sic* *.nat*) order(1.allstock* allstock* VXOmedian VXO* publ* crossborder tender horizontal related2 ind*)  rename(1.Merger Merger 1.Merger#1.VXOmedian Merger*VXOmedian VXOClose VXO) 	eqlabels("Second stage: CARs" "First stage: Selection") label



}

	

	
	
	
	
